from flask import current_app, jsonify
from flask_script import Manager
from flask_migrate import MigrateCommand, Migrate

from info import db, create_app
from info.models import User
from info.utits.response_code import RET

app = create_app("development")

manager = Manager(app)
Migrate(app,db)
manager.add_command("db",MigrateCommand)


@manager.option('-n','--name',dest="name")
@manager.option('-p','--password',dest="password")
def create_admin(name,password):
    if not all([name,password]):
        return "参数不足"

    user = User()
    user.nick_name = name
    user.mobile = name
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR,errmsg="保存管理员账号异常")

    return "创建管理员用户成功"


if __name__ == '__main__':
    manager.run()